import api from "../../utils/api"
import util from "../../utils/util"
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    photos:[],
    currentIndex:1,
    showImage:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取图片
    wx.request({
      url: api.getUserById,
      data: {
        id: app.globalData.openid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (response) {
        console.log("获取个人信息成功！");
        // debugger;
        that.setData({
          photos: response.data.data[0].pic.split("^")
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 上传照片
   */
  addPicture:function(){
    var that = this;
    /*
    wx.chooseImage({
      count:1,
      success: function(res) {
        //上传
        wx.uploadFile({
          url: api.uploadImage,
          filePath: res.tempFiles[0].path,
          header:{
            "content-type":"multipart/form-data"
          },
          name: 'uploadFile',
          success:function(response){
            //更新照片信息
            var tempStr = JSON.parse(response.data).infor;
            console.log(tempStr);
            that.data.photos.push(tempStr);
            that.setData({
              photos:that.data.photos
            });

            //计算拼接字符串
            var str = that.getURLsStr();
            that.uploadImgStr(str);
            //
          }
        })
      },
    })
    */
        util.uploadImage(function(res){
          console.log(res);
          debugger;
          var tempStr = JSON.parse(res.data).infor;
          that.data.photos.push(tempStr);
          that.setData({
            photos: that.data.photos
          });
          //计算拼接字符串
          var str = that.getURLsStr();
          that.uploadImgStr(str);
        });
  },
  /**
   * 左右滑动
   */
  swiperChange:function(e){
    console.log("左右");
    console.log(e);
    var index = e.detail.current;
    this.setData({
      currentIndex:index+1
    });
  },
  /**
   * 隐藏大图
   */
  hideImageBox:function(e){
    this.setData({
      showImage:false
    });

  },
  /**
   * 显示大图
   */
  showImage:function(){
    this.setData({
      showImage:true
    });
  },
  /**
   * 计算imag地址拼接的字符串
   */
  getURLsStr:function(){
    return this.data.photos.join("^");
  },
  /**
   * 上传图片地址信息
   */
  uploadImgStr:function(str){
    wx.request({
      url: api.insertpic,
      method:"POST",
      header:{
        "content-type": "multipart/form-data"
      },
      data:{
        id:app.globalData.openid,
        pic:str
      },
      success:function(response){
        console.log("上传图片");
        console.log(response.data);
      }
    })
  }
})